package org.oos.model;

import java.util.Date;

import org.oos.OOS4Java;
import org.oos.atom.elements.Category;
import org.oos.atom.elements.Text;
import org.oos.atom.entries.ContactEntry;
import org.oos.atom.entries.impl.ContactEntryImpl;
import org.oos.dao.ContactDao;
import org.oos.dao.exceptions.DeleteException;
import org.oos.dao.exceptions.ReadException;
import org.oos.dao.exceptions.UpdateException;
import org.oos.model.UserAvatar.Size;

public class Contact extends AbstractEntityImpl {
	private static final long serialVersionUID = 9192847230584638982L;

	private ContactEntry entry;
	private User user;
	private User contact;

	public Contact(OOS4Java oos4Java, ContactEntry entry) {
		super(oos4Java);
		this.entry = entry;
	}
	
	public Contact(OOS4Java oos4Java, User user, User contact) {
		super(oos4Java);
		this.user = user;
		this.contact = contact;
		
		this.entry = new ContactEntryImpl();
		this.entry.setAuthor(user);
		this.entry.setTitle(new Text(contact.getNick()));
		this.entry.setSlug(contact.getSlug());
		this.entry.setAvatar(contact.getAvatar().getImagePath(Size.LARGE));
		this.setTrusted(false);
	}

	public ContactEntry getEntry() {
		return entry;
	}

	public void setEntry(ContactEntry entry) {
		this.entry = entry;
	}

	public String getContactNick() {
		return entry.getTitle().getValue();
	}

	public void setContactNick(String nick) {
		entry.setTitle(new Text(nick));
	}

	public String getContactSlug() {
		return entry.getSlug();
	}

	public void setContactSlug(String slug) {
		entry.setSlug(slug);
	}

	public UserAvatar getContactAvatar() {
		return new UserAvatar(entry.getAvatar());
	}

	public String getUserNick() {
		return entry.getAuthor().getNick();
	}

	public String getUserSlug() {
		return entry.getAuthor().getSlug();
	}

	public UserAvatar getUserAvatar() {
		return entry.getAuthor().getAvatar();
	}
	
	public Date getDate() {
		return entry.getUpdated();
	}
	
	public void setDate(Date date) {
		entry.setUpdated(date);
	}

	public boolean isTrusted() {
		Category category = entry.getTrusted();
		if (category != null) {
			return category.getTerm().equals("true");
		} else {
			return false;
		}
	}
	
	public void setTrusted(boolean trusted) {
		Category category = Category.create(Category.Type.TRUSTED);
		category.setTerm(trusted?"true":"false");
		
		entry.setTrusted(category);
	}
	
	public User getUser() throws ReadException {
		if (user == null) {
			user = oos4Java.getUser(this.getUserSlug());
		}
		return user;
	}
	
	public User getContact() throws ReadException {
		if (contact == null) {
			contact = oos4Java.getUser(this.getContactSlug());
		}
		return contact;
	}

	public void update() throws UpdateException {
		ContactDao dao = oos4Java.getContactDao();
		dao.setUserSlug(getUserSlug());
		dao.update(entry);
	}

	public void delete() throws DeleteException {
		ContactDao dao = oos4Java.getContactDao();
		dao.setUserSlug(getUserSlug());
		dao.delete(entry);
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Contact) {
			Contact other = (Contact) obj;
			return other.getUserSlug().equals(getUserSlug()) && other.getContactSlug().equals(getContactSlug());
		}
		return false;
	}
}
